define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('F0103Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            /**
             * options : {
             *      wo_no: 工單編號
             *      op_no: 作業站編號
             *      machine_no: 設備編號
             *      position_no: 工位編號
             *      material_no: 物料編號
             *      substitute_material_no: 替代物料編號
             *      material_plot_no: 物料批號
             *      put_in_place_type: 扣料點
             *      callback : function
             * }
            **/
            this.init = function (options) {
                if(options){
                    options.wo_no =  options.wo_no || '';
                    options.op_no = options.op_no || '';
                    options.machine_no = options.machine_no || 'N/A';
                    options.position_no = options.position_no || 'N/A';
                }else{
                    options = {wo_no: '', op_no: '', machine_no: 'N/A', position_no: 'N/A'};
                }
                
                //工單開窗
                function loadWoNoList(condition, callback) {
                    $MMWService.showQueryListPNIG({
                        page_mode: 1,
                        pageRecord: 50,
                        notQuery: false,
                        condition: [{
                            name: $filter('translate')('common.queryList.wo_c'),
                            column_name: 'MONo',
                            query_mode: '2',
                            value: '',
                            modeList: [
                                {name:$filter('translate')('common.report.condition.equal'), code:'0'},
                                {name:$filter('translate')('common.report.condition.nequal'), code:'1'},
                                {name:$filter('translate')('common.report.condition.contain'), code:'2'},
                                {name:$filter('translate')('common.report.condition.ncontain'), code:'3'}
                            ]
                        }],
                        header: [
                            {label: $filter('translate')('common.queryList.wo_c'), code: 'page_no'},
                            {label: $filter('translate')('common.queryList.item_c'), code: 'productno'},
                            {label: $filter('translate')('common.queryList.item_n'), code: 'productname'},
                            {label: $filter('translate')('common.queryList.item_spec'), code: 'productversion'}
                        ],
                        setCondition: function (conditions) {
                            conditions.push({column_name: 'MOSTATE', query_mode: '0', value: 3, merge_condition_model: 'AND'});
                            return conditions;
                        },
                        selectCode: condition.wo_no || '',
                        confirm: function (selectItem) {
                            if (callback)
                                callback(selectItem);
                        }
                    });
                }

                //作業站開窗
                function loadOpNoList(condition, callback) {
                    $rootScope.showQueryList({
                        key: 'page_no',
                        header : [
                            {label: $filter('translate')('common.queryList.op_c'), code: 'page_no'},
                            {label: $filter('translate')('common.queryList.op_n'), code: 'page_name'},
                            {label: $filter('translate')('F0103.queryList.material_opt'), code: 'material_opt_label'}
                        ],
                        query: function(conditions, feedback){
                            var content = {
                                page_mode: '33',
                                query_condition_info: []
                            };
                            content.query_condition_info.push({column_name: 'b.MONo', query_mode: '0', value: condition.wo_no, merge_condition_model: 'AND'});
                            content.query_condition_info.push({column_name: 'c.MaterialOption', query_mode: '1', value: 0, merge_condition_model: 'AND'});

                            pageNumberInfoGet(content, function(data){
                                var op_list = data || [];
                                if(data && data.length > 0){
                                    data.forEach(function(item){
                                        item.material_opt_label = $filter('translate')('F0103.queryList.material_opt_' + item.material_opt);
                                    });
                                }
                                feedback(op_list);
                            });
                        },
                        selectCode: condition.op_no || '',
                        confirm: function(selectItem, dialog){
                            dialog.hide();
                            if (callback) {
                                callback(selectItem);
                            }
                        }
                    });
                }

                //機台開窗
                function loadMachineNoList(condition, callback){
                    $MMWService.showQueryListPNIG({
                        page_mode : '34',
                        header: [
                            {label: $filter('translate')('common.queryList.machine_c'), code: 'page_no'},
                            {label: $filter('translate')('common.queryList.machine_n'), code: 'page_name'}
                        ],
                        setCondition: function (conditions) {
                            conditions.push({column_name:'MONo', query_mode:'0', value: condition.wo_no, merge_condition_model:'AND' });
                            conditions.push({column_name:'OPNo', query_mode:'0', value: condition.op_no, merge_condition_model:'AND' });
                            return conditions;
                        },
                        confirm: function (selectItem) {
                            if (callback) {
                                callback(selectItem);
                            }
                        }
                    });
                }

                //工位開窗
                function loadPositionNoList(condition, callback){
                    $MMWService.showQueryListPNIG({
                        page_mode : '28',
                        header: [
                            {label: $filter('translate')('F0103.queryList.position_no'), code: 'page_no'}
                        ],
                        setCondition: function (conditions) {
                            conditions.push({column_name:'OPNo', query_mode:'0', value: condition.op_no, merge_condition_model:'AND' });
                            return conditions;
                        },
                        confirm: function (selectItem) {
                            if (callback) {
                                callback(selectItem);
                            }
                        }
                    });
                }

                //物料移轉作業
                function materialTransferProcessImp(dialog, callback){
                    var content = {
                        material_no: options.material_no,
                        substitute_material_no: options.substitute_material_no,
                        material_plot_no: options.material_plot_no,
                        put_in_place_type: options.put_in_place_type,
                        source_detail: [],
                        target_detail: []
                    };
                    content.source_detail.push({
                        wo_no: dialog.sourceItem.wo_no,
                        op_no: dialog.sourceItem.op_no,
                        machine_no: dialog.sourceItem.machine_no,
                        position_no: dialog.sourceItem.position_no,
                    });
                    content.target_detail.push({
                        wo_no: dialog.targetItem.wo_no,
                        op_no: dialog.targetItem.op_no,
                        machine_no: dialog.targetItem.machine_no,
                        position_no: dialog.targetItem.position_no,
                    });

                    materialTransferProcess(content, function(){
                        if (callback)
                            callback();
                    });
                }
                
                function materialTransferProcess(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_MAT.material_transfer_process',
                        content : content,
                        success : function(data, response) {
                            $rootScope.showToast(response.Message);
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function pageNumberInfoGet(content, callback){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SYS.page_number_info_get',
                        content : content,
                        success : function(data){
                            if (callback)
                                callback(data.page_number_info_get);
                        }
                    });
                }

                function beforeConfirm(dialog, callback){
                    var msg = '';
                    if(dialog.targetItem.wo_no === '' || dialog.targetItem.op_no === ''){
                        msg = $filter('translate')('F0103.msg.wo_and_op_empty');
                    }else if(dialog.targetItem.material_opt == '1' && dialog.targetItem.machine_no == 'N/A'){
                        msg = $filter('translate')('F0103.msg.machine_no_empty');
                    }else if(dialog.targetItem.material_opt == '4' && dialog.targetItem.position_no == 'N/A'){
                        msg = $filter('translate')('F0103.msg.position_no_empty');
                    }
                    if(msg != ''){
                        $rootScope.showAlert(msg);
                    }else{
                        if(callback)
                            callback();
                    }
                }

                function beforeOpenDialog(callback){
                    if(callback)
                        callback();
                }

                function openDialog(materialList){
                    $mdDialog.dialog(
                        'module/moduleF/program/F0103/F0103.html',
                        function (dialog) {
                            return {
                                css_url:'module/moduleF/program/F0103/F0103.css',
                                sourceItem : options || {},
                                targetItem: {wo_no: '', op_no: '', machine_no: 'N/A', position_no: 'N/A'},
                                loadWoNoList: function () {
                                    // if (dialog.targetItem.wo_no.length < 5) {
                                    //     $rootScope.showAlert($filter('translate')('F0103.msg.wo_no_length'));
                                    // } else {
                                        loadWoNoList(dialog.targetItem, function (data) {
                                            dialog.targetItem.wo_no = data.page_no;
                                            dialog.targetItem.item_no = data.productno;
                                            dialog.targetItem.op_no = '';
                                            dialog.targetItem.machine_no = 'N/A';
                                            dialog.targetItem.position_no = 'N/A';
                                            dialog.targetItem.material_opt = '';
                                        });
                                    // }
                                },
                                loadOpNoList: function () {
                                    if (dialog.targetItem.wo_no == '') {
                                        $rootScope.showAlert($filter('translate')('F0103.msg.wo_no_empty'));
                                    } else {
                                        loadOpNoList(dialog.targetItem, function (data) {
                                            dialog.targetItem.op_no = data.page_no;
                                            dialog.targetItem.material_opt = data.material_opt;
                                            if(data.material_opt == '1'){ // 1: 設備上料
                                                dialog.targetItem.position_no = 'N/A';
                                            }else if(data.material_opt == '3'){ // 3: 作業站上料
                                                dialog.targetItem.machine_no = 'N/A';
                                                dialog.targetItem.position_no = 'N/A';
                                            }
                                        });
                                    }
                                },
                                loadMachineNoList: function () {
                                    if (dialog.targetItem.wo_no == '') {
                                        $rootScope.showAlert($filter('translate')('F0103.msg.wo_no_empty'));
                                    } else if (dialog.targetItem.op_no == '') {
                                        $rootScope.showAlert($filter('translate')('F0103.msg.op_no_empty'));
                                    } else {
                                        loadMachineNoList(dialog.targetItem, function(data){
                                            dialog.targetItem.machine_no = data.page_no;
                                        });
                                    }
                                },
                                loadPositionNoList: function () {
                                    if (dialog.targetItem.wo_no == '') {
                                        $rootScope.showAlert($filter('translate')('F0103.msg.wo_no_empty'));
                                    } else if (dialog.targetItem.op_no == '') {
                                        $rootScope.showAlert($filter('translate')('F0103.msg.op_no_empty'));
                                    } else {
                                        loadPositionNoList(dialog.targetItem, function(data){
                                            dialog.targetItem.position_no = data.page_no;
                                        });
                                    }
                                },
                                confirm: function(){
                                    beforeConfirm(dialog, function(){
                                        materialTransferProcessImp(dialog, function(){
                                            dialog.hide();
                                            if (options.callback)
                                                options.callback();
                                        });
                                    });

                                },
                                back: function () {
                                    dialog.hide();
                                }
                            };
                        }
                    );
                }

                beforeOpenDialog(function(){
                    openDialog();
                });
            };
        }]);
});
